class MyPageHeader extends BaseEditMaster {
    constructor(dataId, currentTaskPrivileges, nextTaskPrivileges, pageStatus, pageListData,
            returnUrl, fetchDataUrl, saveUrl, exportUrl, printUrl) {
        super(dataId, currentTaskPrivileges, nextTaskPrivileges, pageStatus, pageListData,
                returnUrl, fetchDataUrl, saveUrl, exportUrl, printUrl);
    }
    // 得到数据
    GetEntityData() {
        var item = {};
        item['employeeId'] = $("#EmployeeId").val();
        item['employeeNo'] = $.trim($("#EmployeeNo").val());
        item['employeeName'] = $.trim($("#EmployeeName").val());
        item['gender'] = $("#Gender").val();
        item['birthday'] = parent.formatNullInputValue($.trim($("#Birthday").val()));
        item['identityCard'] = $.trim($("#IdentityCard").val());
        item['telephone'] = $.trim($("#Telephone").val());
        item['departmentId'] = $("#DepartmentId").val();
        item['jobName'] = $.trim($("#JobName").val());
        item['hiredDate'] = parent.formatNullInputValue($.trim($("#HiredDate").val()));
        item['dimissionDate'] = parent.formatNullInputValue($.trim($("#DimissionDate").val()));
        item['description'] = parent.formatNullInputValue($.trim($("#Description").val()));
        item['isMembership'] = 1;
        item['createdBy'] = $("#UserId").val();
        item['updatedBy'] = $("#UserId").val();

        return JSON.stringify(item);
    }
    FillData(data) {
        $("#EmployeeId").val(data.employeeId);
        $("#EmployeeNo").val(data.employeeNo);
        $("#EmployeeName").val(data.employeeName);
        $("#Gender").val(data.gender);
        $("#Birthday").val(data.birthday);
        $("#IdentityCard").val(data.identityCard);
        $("#Telephone").val(data.telephone);
        $("#DepartmentId").val(data.departmentId);
        $("#DepartmentName").val(data.departmentName);
        $("#JobName").val(data.jobName);
        $("#HiredDate").val(data.hiredDate);
        $("#DimissionDate").val(data.dimissionDate);
        $("#Description").val(data.description);

        $("#NodeText").val(data.departmentName);

        // 最后的审核人、审核日期
        //$("#CheckedBy").val(item.checkedBy);
        //$("#CheckedByName").val(item.checkedByName);
        //$("#CheckedDate").val(item.checkedDate === null ? '' : item.checkedDate);
        //$("#CheckedStatus").val(item.checkedStatus);
        //$('#FlowOrderNo').val(item.flowOrderNo);
        this.currentTaskPrivileges = data.currentTaskPrivileges;
        this.nextTaskPrivileges = data.nextTaskPrivileges;
        this.unCheckFlag = data.unCheckFlag;

        this.SetPageElementStatus();
        $("#EmployeeNo").focus();
    }
    SetPageElementStatus() {
        super.SetPageElementStatus(function () {
            //$("#ButSave").hide();
        }, function () {
            //$("#ButSave").show();
        }, function () {

        });
    }
    Save(flag) {
        super.Save(function (data, $self) {
            if (data > 0) {
                if (parseInt($self.pageStatus) === 1) {
                    $("#EmployeeId").val(data);   // 设置刚才插入的记录ID
                    $self.dataId = data;
                    $self.pageStatus = 2;
                }
                if (flag === 0) {
                    $self.New();
                }
            }
        });
    }
    OnNew() {
        // 默认设置一些值
        $('#EmployeeId').val("0");
        super.OnNew();
        // 初始化所属材料分类
        $('#DepartmentId').val($("#NodeId").val());
        $('#DepartmentName').val($("#NodeText").val());
        $("#EmployeeNo").focus();
    }
    DoChoiceDept() {
        let _choice = new CommonChoice();
        _choice.DoChoiceCatalogue('/master/department/choice.html', '选择所属部门', (data) => {
            if (data) {
                // 判断是否数据发生了改变如果改变则设置改变标记
                if ($("#DepartmentName").val() !== data[1]) {
                    $("#form_data").data("changed", true);
                }
                $("#DepartmentId").val(data[0]);
                $("#DepartmentName").val(data[1]);
                $("#DepartmentName").focus();
            }
        });
    }
}

var _InstancePageHeader = null;

//-----------------------------------------------------------------------------
// 页面初始化
//-----------------------------------------------------------------------------
function onPageReady() {
    let _pageStatus = $("#Action").val();
    let _dataId = $("#EmployeeId").val();

    let employee_list_result = $.cookie("employee_list_result") === undefined ? "" : $.cookie("employee_list_result");
    let _pageListData = [];
    if (employee_list_result !== "") {
        _pageListData = employee_list_result.split(',');
        $("#rec_total_rows").text(_pageListData.length);
        $("#rec_cur_row").text($.inArray(_dataId, _pageListData) + 1);

        if (_pageListData.length > 0) {
            let index = $.inArray(_dataId, _pageListData);
            if (index === 0) {
                $("#ButPrev").attr("disabled", "disabled");
            } else {
                $("#ButNext").removeAttr("disabled");
            }
            if (index === (_pageListData.length - 1)) {
                $("#ButNext").attr("disabled", "disabled");
            } else {
                $("#ButPrev").removeAttr("disabled");
            }
        }
    } else {
        $("#rec_total_rows").text("1");
        $("#rec_cur_row").text("1");
    }

    _InstancePageHeader = new MyPageHeader(_dataId, 0, 0, _pageStatus, _pageListData,
            "/master/employee/list.html",
            "/master/employee/fetchByKey.html",
            "/master/employee/save.html",
            "/master/employee/export.html",
            "/master/employee/print.html");

    window.laydate.render({
        elem: '#Birthday',
        theme: '#393D49',
        trigger: 'click'
    });
    window.laydate.render({
        elem: '#HiredDate',
        theme: '#393D49',
        trigger: 'click'
    });
    window.laydate.render({
        elem: '#DimissionDate',
        theme: '#393D49',
        trigger: 'click'
    });

    _InstancePageHeader.OnPageLoad();
}

// 画面大小或者位置移动
function onPageResize() {
    var w = $(window).width();
    var h = $(window).height();

    $(".data__table_wrapper").css({width: w - 40, height: h - 120});
}

//-----------------------------------------------------------------------------
// 验证函数
//-----------------------------------------------------------------------------
$.validator.setDefaults({
    ignore: 'hidden, [reaonly=readonly]'
});
$.validator.addMethod("valueNotEquals", function (value, element, arg) {
    return arg !== value;
}, "Value must not equal arg.");

$(function () {
    $("#form_data").validate({
        // Step 1：设置验证规则和错误消息
        rules: {
            _UserName: "required",
            _EmployeeName: "required",
            _Birthday: "dateISO",
            _HiredDate: "dateISO",
            _DepartmentName: "required",
            _DimissionDate: "dateISO"
        },
        messages: {
            _UserName: "请填写人员编号！",
            _EmployeeName: "请填写人员名称！",
            _Birthday: "请输入正确的日期！",
            _HiredDate: "请输入正确的日期！",
            _DepartmentName: "请输入所属部门！",
            _DimissionDate: "请输入正确的日期！"
        },
        // Step 2：设置验证触发事件
        onclick: false,
        onfocusout: false,
        onfocusin: false,
        onkeyup: false,
        onkeydown: false,
        focusInvalid: false,
        // Step 3：设置错误信息提示
        showErrors: window.showErrors  //使用自定义的错误显示方法
    });
});
